採用接 cloud function 再打 job
建立一個 python job image 用於觸發
快速建立一個 python main.py 可以打印 log 的 file
import logging
import time
# 配置日誌
logging.basicConfig(level=logging.INFO, format='%(asctime)s - %(message)s')
def main():
logging.info("開始執行腳本")
for i in range(5):
logging.info(f"這是第 {i+1} 條日誌訊息")
time.sleep(1) # 暫停1秒,模擬處理時間
logging.info("腳本執行完畢")
if __name__ == "__main__":
main()
dockerfile
FROM python:3.11-slim
WORKDIR /app
COPY main.py .
CMD ["python", "main.py"]
docker build
docker build -t python-logger:v1 .
# Test docker
docker run python-logger:v1
2024-10-05 04:48:17,651 - 開始執行腳本
2024-10-05 04:48:17,653 - 這是第 1 條日誌訊息
2024-10-05 04:48:18,657 - 這是第 2 條日誌訊息
2024-10-05 04:48:19,662 - 這是第 3 條日誌訊息
2024-10-05 04:48:20,664 - 這是第 4 條日誌訊息
2024-10-05 04:48:21,670 - 這是第 5 條日誌訊息
2024-10-05 04:48:22,672 - 腳本執行完畢
create artifact hub with terraform
# install gcloud from the site: https://cloud.google.com/sdk/docs/install
gcloud init
gcloud auth application-default login
# 添加 Artifact Registry 倉庫
resource "google_artifact_registry_repository" "my_repo" {
location = var.region
repository_id = "test-k8s-job"
description = "My artifact repository"
format = "DOCKER"
}
docker push to hub
#!/bin/bash
# 設置變量
PROJECT_ID="your-project-id" # 替換為您的 GCP 項目 ID
REGION="asia-east1" # 替換為您的目標區域
REPO_NAME="test-k8s-job"
IMAGE_NAME="python-logger"
IMAGE_TAG="v1"
# 配置 Docker 以使用 gcloud 作為憑證助手
# gcloud auth configure-docker ${REGION}-docker.pkg.dev
# 標記映像
docker tag ${IMAGE_NAME}:${IMAGE_TAG} ${REGION}-docker.pkg.dev/${PROJECT_ID}/${REPO_NAME}/${IMAGE_NAME}:${IMAGE_TAG}
# 推送映像
docker push ${REGION}-docker.pkg.dev/${PROJECT_ID}/${REPO_NAME}/${IMAGE_NAME}:${IMAGE_TAG}
echo "Image pushed successfully to Artifact Registry"
建立一個 cloud function 用於觸發 job in k8s
在 k8s 上運行 image 看看
gcloud components install gke-gcloud-auth-plugin
gcloud container clusters get-credentials portal-uat-asia-east1-gke --region asia-east1 --project xxx
由於 cluster 是 private,所以要用跳板機。中間安裝流程直接跳過。
gcloud compute ssh portal-uat-asia-east1-bastion-vm --tunnel-through-iap --project=xxx
# 確保最後可以拿到此資料,證明連線成功
kubectl get ns
新增 job
apiVersion: batch/v1
kind: Job
metadata:
name: python-logger-job
spec:
template:
spec:
containers:
- name: python-logger
image: asia-east1-docker.pkg.dev/shopeetwbi/test-k8s-job/python-logger:v1
# 如果您的 Python 腳本需要參數,可以在這裡添加
# args: ["arg1", "arg2"]
restartPolicy: Never
backoffLimit: 4
kubectl apply -f python-logger-job.yaml
由於我的 gke 是 autopilot,剛起 job 需要分配 node 會 pending 一段時間
Events:
Type Reason Age From Message
---- ------ ---- ---- -------
Normal TriggeredScaleUp 41s cluster-autoscaler pod triggered scale-up: [{https://www.googleapis.com/compute/v1/projects/xxxx/zones/asia-east1-c/instanceGroups/gk3-portal-uat-asia-east-nap-rurfc12x-61262313-grp 0->1 (max: 1000)}]
Warning FailedScheduling 4s (x11 over 80s) gke.io/optimize-utilization-scheduler no nodes available to schedule pods
完成後得到 logs
kubectl logs python-logger-job-ll8bg
exec /usr/local/bin/python: exec format error
這可能是因為 arm 打包 image 的問題,我們改成用 gcloud building
gcloud builds submit --pack image=asia-east1-docker.pkg.dev/shopeetwbi/test-k8s-job/python-logger:v1